﻿using GKQC.DbHelper.ViewModels;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace GKQC.DbHelper.DbModels
{
    /// <summary>
    /// 信赖性检测单单头
    /// </summary>
    [SugarTable("t_ort_head")]
    public class OrtHeadModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public OrtHeadModel()
        {
            this.Dtls = new List<OrtDtlModel>();
            this.Pn = string.Empty;
            this.Customer = string.Empty;
            this.CustomerPn = string.Empty; ;
            this.LotNo = string.Empty;
            this.SourceCode = string.Empty;
            this.SourceNo = string.Empty;
            this.SourceSNo = 1;
            this.Remark = string.Empty;
            this.TestDate = DateTime.Now;
            this.ExpireDate = DateTime.Now;
            this.FinishDate = DateTime.Parse("2099-12-31");
            this.LotNo = string.Empty;
            this.LcrMeter = "LCR11022";
            this.LcMeter = "CH-2311";
            this.Standard = new StandardView();
            this.IsValid = true;
            this.Size = string.Empty;
            this.FoilP = string.Empty;
            this.FoilN = string.Empty;
            this.LeadWire = string.Empty;
            this.Eps = string.Empty;
            this.Paste = string.Empty;
            this.Rubber = string.Empty;
            this.AlCase = string.Empty;
            this.BoxNo = string.Empty;
            this.Series = string.Empty;
            this.CustomerCode = string.Empty;
            this.Level = "A";
            this.CapMin = -20;
            this.CapMax = 20;
            this.SampleDate = DateTime.Now;
            this.DateCode = string.Empty;
            this.CapRate = 25;
            this.FoilpSupplier = string.Empty;
        }

        public string FoilpSupplier { get; set; }

        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public long Id { get; set; }

        /// <summary>
        /// 专案编号
        /// </summary>		
        public string ProNo { get; set; } = "";
        /// <summary>
        /// 专案名称
        /// </summary>		
        public string ProName { get; set; } = "";


        /// <summary>
        /// DMax
        /// </summary>
        public double DMax { get; set; } = 2;

        /// <summary>
        ///LMax
        /// </summary>
        public double LMax { get; set; } = 2;

        /// <summary>
        /// FMax
        /// </summary>
        public double FMax { get; set; } = 2;

        /// <summary>
        /// LcTime充电时间 分钟
        /// </summary>
        public int LcTime { get; set; } = 2;

        /// <summary>
        /// 出货数量
        /// </summary>
        public double DeliveryQty { get; set; }


        /// <summary>
        /// 耐压测试结果
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public List<OrtVoltageModel> Volts { get; set; } = new List<OrtVoltageModel>();

        public void GenVolts()
        {
            if (Volts.Count > 0) return;
            for (int i = 0; i < 10; i++)
            {
                OrtVoltageModel v = new OrtVoltageModel();
                Volts.Add(v);
            }
        }

        public string BadReason { get; set; } = string.Empty;
        public string BadDesc { get; set; } = string.Empty;

        /// <summary>
		/// 试验单号
        /// </summary>		
        public string BillNo { get; set; }
        /// <summary>
        /// 批号
        /// </summary>		
        public string LotNo { get; set; }
        /// <summary>
        /// 序号
        /// </summary>		
        public int SNo { get; set; }
        /// <summary>
        /// 来源单别
        /// </summary>		
        public string SourceCode { get; set; }
        /// <summary>
        /// 来源单号
        /// </summary>		
        public string SourceNo { get; set; }
        /// <summary>
        /// 来源单项次
        /// </summary>		
        public int SourceSNo { get; set; }
        /// <summary>
        /// 客户
        /// </summary>		
        public string Customer { get; set; }
        /// <summary>
        /// 代号
        /// </summary>		
        public string CustomerCode { get; set; }
        /// <summary>
        /// 容量
        /// </summary>		
        public double Cap { get; set; }
        /// <summary>
        /// 电压
        /// </summary>		
        public double Voltage { get; set; }
        /// <summary>
        /// 正箔
        /// </summary>		
        public string FoilP { get; set; }
        /// <summary>
        /// 负箔
        /// </summary>		
        public string FoilN { get; set; }
        /// <summary>
        /// 导针/导箔
        /// </summary>		
        public string LeadWire { get; set; }
        /// <summary>
        /// 电解纸
        /// </summary>		
        public string Eps { get; set; }
        /// <summary>
        /// 电解液
        /// </summary>		
        public string Paste { get; set; }
        /// <summary>
        /// 胶盖/盖板
        /// </summary>		
        public string Rubber { get; set; }
        /// <summary>
        /// 铝壳
        /// </summary>		
        public string AlCase { get; set; }

        /// <summary>
        /// 容量上限 (%)
        /// </summary>		
        public double CapMax { get; set; }

        /// <summary>
        /// 容量下限 (%)
        /// </summary>		
        public double CapMin { get; set; }

        /// <summary>
        /// 容量变化率 (%)
        /// </summary>		
        public double CapRate { get; set; }
        /// <summary>
        /// 容量變化率2
        /// </summary>
        public double CapRate2 { get; set; }

        /// <summary>
        /// DF初值(%)
        /// </summary>		
        public double DfMax { get; set; }
        /// <summary>
        /// DF末值(%)
        /// </summary>		
        public double DfMaxAfter { get; set; }
        /// <summary>
        /// LC最大值(μA)
        /// </summary>		
        public double LcMax { get; set; }
        /// <summary>
        /// LC最大值(μA)
        /// </summary>		
        public double LcMaxAfter { get; set; }
        /// <summary>
        /// Z值上限(mΩ)
        /// </summary>		
        public double ZMax { get; set; }
        /// <summary>
        /// 阻抗比率1(%)
        /// </summary>		
        public double ZRate { get; set; }
        /// <summary>
        /// 阻抗比率2(%)
        /// </summary>		
        public double ZRate2 { get; set; }
        /// <summary>
        /// ESR初期值(mΩ)
        /// </summary>		
        public double EsrMax { get; set; }
        /// <summary>
        /// ESR试验后值(mΩ)
        /// </summary>		
        public double EsrMaxAfter { get; set; }
        /// <summary>
        /// 荷重(kg)
        /// </summary>
        public double WeightMax { get; set; }
        /// <summary>
        /// 试验温度
        /// </summary>		
        public double TestTemp { get; set; }
        /// <summary>
        /// 试验电压
        /// </summary>		
        public double TestVoltage { get; set; }
        /// <summary>
        /// 纹波电流(A)
        /// </summary>		
        public double RippleCurrent { get; set; }
        /// <summary>
        /// 试验总时间
        /// </summary>		
        public double TestTotalTime { get; set; }
        /// <summary>
        /// 箱号
        /// </summary>		
        public string BoxNo { get; set; }
        /// <summary>
        /// 完成日期
        /// </summary>		
        public DateTime FinishDate { get; set; }
        /// <summary>
        /// 更新日期
        /// </summary>		
        public DateTime? ModiDate { get; set; }
        /// <summary>
        /// 建立日期
        /// </summary>		
        public DateTime CreateDate { get; set; }
        /// <summary>
        /// 試驗日期
        /// </summary>		
        public DateTime TestDate { get; set; }
        /// <summary>
        /// 取樣日期
        /// </summary>		
        public DateTime SampleDate { get; set; }
        /// <summary>
        /// 系列
        /// </summary>		
        public string Series { get; set; }
        /// <summary>
        /// 尺寸
        /// </summary>		
        public string Size { get; set; }
        /// <summary>
        /// 料号
        /// </summary>		
        public string Pn { get; set; }
        /// <summary>
        /// 客户料号
        /// </summary>		
        public string CustomerPn { get; set; }
        /// <summary>
        /// 建立人
        /// </summary>		
        public string CreateUser { get; set; }
        /// <summary>
        /// 报表ID
        /// </summary>		
        public int ReportId { get; set; }
        /// <summary>
        /// LCR仪器
        /// </summary>		
        public string LcrMeter { get; set; }
        /// <summary>
        /// LC仪器
        /// </summary>		
        public string LcMeter { get; set; }
        /// <summary>
        /// 其他仪器
        /// </summary>		
        public string OtherMachine { get; set; }
        ///// <summary>
        ///// 试验类型代号
        ///// </summary>		
        //public string TestType { get; set; }
        /// <summary>
        /// 试验频率(Hz)
        /// </summary>		
        public string TestFreq { get; set; }
        /// <summary>
        /// 缺陷级别
        /// </summary>		
        public string Level { get; set; }
        /// <summary>
        /// 电气检测数量
        /// </summary>		
        public int ElectricQty { get; set; }
        /// <summary>
        /// 尺寸检测数量
        /// </summary>		
        public int SizeQty { get; set; }
        /// <summary>
        /// 修改人
        /// </summary>		
        public string ModiUser { get; set; }

        /// <summary>
        /// 單據類型
        /// </summary>
        public string BillType { get; set; }
        /// <summary>
        /// 試驗時間
        /// </summary>
        public int TestTime { get; set; }
        /// <summary>
        /// 到期日期
        /// </summary>
        public DateTime? ExpireDate { get; set; }

        /// <summary>
        /// 備註
        /// </summary>
        public string Remark { get; set; }
        /// <summary>
        /// 測試目的
        /// </summary>
        public string TestTarget { get; set; }

        /// <summary>
        /// 審核日期
        /// </summary>
        public DateTime? ApproveDate  { get; set; }
        /// <summary>
        /// 審核人
        /// </summary>
        public string ApproveUser { get; set; }

        /// <summary>
        /// cp线直径（引脚直径d）
        /// </summary>
        public double cp { get; set; }

        /// <summary>
        /// 是否合格
        /// </summary>
        public bool IsValid { get; set; }

        /// <summary>
        /// 试验湿度
        /// </summary>
        public double TestHumidity { get; set; }


        /// <summary>
        /// 周藩
        /// </summary>		
        public string DateCode { get; set; }

        /// <summary>
        /// 獲取尺寸中的本體直徑D ,尺寸格式應為：D*L
        /// </summary>
        /// <returns></returns>
        public double GetD()
        {
            double D = 0;
            if(!string.IsNullOrWhiteSpace(this.Size) && this.Size.Contains("*"))
            {
                string[] ss = this.Size.Split('*');
                if(!string.IsNullOrWhiteSpace(ss[0].Trim()))
                    double.TryParse(ss[0].Trim(), out D);
            }
            return D;
        }

        /// <summary>
        /// 獲取尺寸中的本體高度L ,尺寸格式應為：D*L
        /// </summary>
        /// <returns></returns>
        public double GetL()
        {
            double L = 0;
            if (!string.IsNullOrWhiteSpace(this.Size) && this.Size.Contains("*"))
            {
                string[] ss = this.Size.Split('*');
                if (!string.IsNullOrWhiteSpace(ss[1].Trim()))
                    double.TryParse(ss[01].Trim(), out L);
            }
            return L;
        }

        public string GetSpec()
        {
            return this.Cap.ToString() + "μF/" + this.Voltage.ToString(0 + "V");
        }

        /// <summary>
        /// 是否固态，固态用ESR 液态用Z
        /// </summary>
        public bool IsSolid()
        {
            if (this.BillType == Enum.GetName(typeof(OrtBillType), OrtBillType.PRC)
                        || this.BillType == Enum.GetName(typeof(OrtBillType), OrtBillType.LDP)//LDP
                        || this.BillType == Enum.GetName(typeof(OrtBillType), OrtBillType.LDR)//LDR/LDW
                        || this.BillType == Enum.GetName(typeof(OrtBillType), OrtBillType.SFLDP)//SFLDP
                        || this.BillType == Enum.GetName(typeof(OrtBillType), OrtBillType.SFLDR)//SFLDR/SFLDW
                        || this.BillType == Enum.GetName(typeof(OrtBillType), OrtBillType.YGR)  //YGR
                        || this.BillType == Enum.GetName(typeof(OrtBillType), OrtBillType.YGL) //YGL
                        || this.BillType == Enum.GetName(typeof(OrtBillType), OrtBillType.SFYGR)  //SFYGR
                        || this.BillType == Enum.GetName(typeof(OrtBillType), OrtBillType.SFYGL)  //SFYGL
                )
                return true;
            else return false;
        }

        [SugarColumn(IsIgnore = true)]
        public List<OrtDtlModel> Dtls { get; set; }
        [SugarColumn(IsIgnore = true)]
        public StandardView Standard { get; set; }

        /// <summary>
        /// 是否是批量导出单独的报表，批量导出时excel命名要加Id, 避免重名
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public bool IsBatchExport { get; set; }
    }
}
